<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Patient Account</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/search.js"></script>


<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav");


	$(document).ready(function(){
		var datepicker_CurrentInput;  
        $.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        $(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		$("#birth").datepicker({
			dateFormat:'dd-mm-yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});

	function setIsPay(appointmentNo, isPay, isPayId){
		var isPayStatus;
		if( isPay == true ){
			isPayStatus = 1;
		}else{
			isPayStatus = 0;
		}
		jQuery.ajax({type:'post',async:'false', url:'set_pay.php', data:"appointmentNo=" + appointmentNo + "&isPayStatus=" + isPayStatus, dataType:'json', success:function(result){
				if( result.data.status == "success" ){
					
					alert(result.data.msg);
				}else{
					if( isPay == true ){
						document.getElementById(isPayId).checked = false;
					}else{
						document.getElementById(isPayId).checked = true;
					}
					alert(result.data.msg);
					
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				if( isPay == true ){
					document.getElementById(isPayId).checked = false;
				}else{
					document.getElementById(isPayId).checked = true;
				}
				alert(XMLHttpRequest.responseText);
			}
				
		});
		
		
	}
		
</script>



</head>
<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php include_once 'search_patient.php';?>
		<?php
		
		
		
			$appointmentResult;
			$selectCount = 0;
			$patientFirstName = "";
			$patientSurname = "";
			if( isset($_GET["patientNo"]) && strcmp(trim($_GET["patientNo"]), "" ) != 0 && preg_match("/^\d+$/", $_GET["patientNo"]) == true ){
				$patientNo = $_GET["patientNo"];
				
				$ini_array = parse_ini_file("config/config.ini");
				date_default_timezone_set($ini_array["default_timezone"]);
				$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
				mysqli_set_charset($conn, "utf8");
				ini_set('default_charset', "UTF-8");
				$patientNo = mysqli_real_escape_string($conn, $patientNo);
				//echo $patientNo."patientNo";
				$appointmentSql = "SELECT AA.appointment_no, AA.appointment_datetime, AA.type, V.`isPay`, V.`Professional_fee` FROM visit AS V RIGHT OUTER JOIN (SELECT A.`appointment_no`, A.`appointment_datetime`,A.`type` FROM appointment AS A, patient AS P  WHERE  A.`Patient_no` = P.`Patient_no` AND P.`Patient_no`=".$patientNo.") AS AA ON AA.appointment_no=V.`Appointment_no`";
				$appointmentResult = mysqli_query($conn, $appointmentSql);
				$selectCount = mysqli_num_rows($appointmentResult);
				
				$patientInfoSql = "select first_name, surname from patient where patient_no=".$patientNo;
				$patientResult = mysqli_query($conn, $patientInfoSql);
				if( $row = mysqli_fetch_array($patientResult) ){
					$patientFirstName = $row["first_name"];
					$patientSurname = $row["surname"];
				}else{
					echo "<script>alert('this patient No. do not exsit in database!!!');window.location.href='index.php'</script>";
				}
				
			}else{
				echo "<script>alert('no patient No. or invalid!!!');window.location.href='index.php'</script>";
			}
	


		?>
		<div id="main_content">
			<div style="margin:0px 80px;display:inline-block;"><label style="font-weight:bold;"><?php echo $patientFirstName."  ".$patientSurname?>:</label></div>
			<div id="tableHead" style="position:relative;left:60px;bottom:1em;margin:0px auto;;width:710px;padding:0px 40px;">
				
				<div style="width:120px;display:inline-block;text-align:center;"><label>Appointment No.</label></div>
				<div style="width:120px;display:inline-block;text-align:center;"><label>Date</label></div>
				<div style="width:60px;display:inline-block;text-align:center;"><label>Time</label></div>
				<div style="width:120px;display:inline-block;text-align:center;"><label>Doctor</label></div>
				<div style="width:60px;display:inline-block;text-align:center;"><label>Fee</label></div>
				<div style="width:30px;display:inline-block;text-align:center;"><label>Paid</label></div>
				<div style="width:90px;display:inline-block;text-align:center;"><label>type</label></div>
			</div>
			<div id="operateList" style="position:relative;left:40px;background:rgb(255,255,255);background:rgba(255,255,255,0.5);width:200px;height:150px;padding:10px 5px;">
				<div style="margin:10px 10px;"><a href="patient_info.php?patient_no=<?php echo $patientNo?>">View Patient Details</a></div>
				<div style="margin:10px 10px;"><a href="appointment.php?patient_no=<?php echo $patientNo?>">New Appointment</a></div>
				<div style="margin:10px 10px;"><a href="statement_account.php?patientNo=<?php echo $patientNo?>">Account Statement</a></div>
				<div style="margin:10px 10px;"><a href="sms_message.php?patientNo=<?php echo $patientNo?>">Send SMS Message</a></div>
				<div style="margin:10px 10px;"><a href="letter_to_clinic_pdf.php?patientNo=<?php echo $patientNo?>">Letter to GP</a></div>
			</div>
			<div id="patientAppointList" style="margin:10px auto;width:600px;position:relative;top:-140px;left:60px;height:600px;overflow:auto;">
				<table style="width:580px">
					<?php 
						
							while($row = mysqli_fetch_array($appointmentResult) ){
								$appointmentNo = $row["appointment_no"];
								$datetime = $row["appointment_datetime"];
								
								$fee = $row["Professional_fee"];
								$isPay = $row["isPay"];
								$type = $row["type"];
								$typeStr = "";
								if( $type==1 ){
									$typeStr="unsigned";
								}else if( $type ==2 ){
									$typeStr="signed";
								}else if( $type == 3 ){
									$typeStr="visited";
								}else if( $type == 4 ){
									$typeStr="remind";
								}else{
									$typeStr="unknown";
								}
						
						
					?>
					<tr>
						<td style="width:20%"><a href="appointment.php?appointment_no=<?php echo $appointmentNo?>"><?php echo $appointmentNo?></a></td>
						<td style="width:20%"><label><?php echo date("d/m/Y", strtotime($datetime))?> </label></td>
						<td style="width:10%"><label><?php echo date("H:i", strtotime($datetime))?></label></td> 
						<td style="width:20%"><label><?php echo "Geraldine"?> </label></td>
						<td style="width:10%"><label><?php echo $fee?></label></td>
						<td style="width:5%"><input type="checkbox" name="ispay" id="isPay<?php echo $appointmentNo?>" <?php if( $isPay==true ){echo "checked=\"checked\"";}?> onclick="<?php if( $type == 3 ){echo "return setIsPay('".$appointmentNo."', this.checked, this.id)";}else{echo "return false";}?>;"/></td>
						<td style="width:15%;text-align:center;"><label><?php echo $typeStr?></label></td>
					</tr>
					<?php
							}
					?>
				</table>
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
